<template>
	<el-container>
		<el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="createComExpense">创建</el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
			<div class="right-panel">
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="recordId" @selection-change="selectionChange" stripe remoteSort remoteFilter>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="预算标题" prop="title" width="200" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="年度" prop="expenseYear" width="100"></el-table-column>
				<el-table-column label="预算金额" prop="amount" width="150"></el-table-column>
				<el-table-column label="余额" prop="balance" width="150"></el-table-column>
				<el-table-column label="是否启用" prop="status" width="100">
					<template #default="scope">
						<el-tag v-if="scope.row.status==0" type="warning">禁用</el-tag>
						<el-tag v-else-if="scope.row.status==1" type="success">启用</el-tag>
						<el-tag v-else type="info">未知</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="150"></el-table-column>
				<el-table-column label="创建人" prop="createUserName" width="100"></el-table-column>
				<el-table-column label="备注说明" prop="remark" width="300" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="150">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import saveDialog from './csave'
export default {
	name: 'Expense Com Config Setting',
	components: {
		scSelectFilter,
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			filterData: [
				{
					title: "预算年度",
					key: "expenseYear",
					multiple: false,
					options: [{
						label: "全部",
						value: ""
					},]
				}
			],
			apiObj: this.$API.finance.expense.getExpenseComConfigList,
			selection: [],
			search: {
				expenseYear:'',
				status:'',
				keyword: null
			}
		}
	},
	mounted() {
		this.getExpenseYear();
	},
	methods: {
		getExpenseYear()
		{
			let yearArr=[];
			let currentYear = parseInt(new Date().getFullYear());
			for(let i=0;i<10;i++)
			{
				let year = currentYear-i;
				let tempJson={
					value:year,
					label:year+"年度"
				};
				yearArr.push(tempJson);
			}
			this.filterData[0].options=this.filterData[0].options.concat(yearArr);
		},
		//添加
		createComExpense() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//删除
		async table_del(row) {
			var reqData = {recordId: row.recordId}
			var res = await this.$API.finance.expense.deleteExpenseComConfig.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var recordIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					recordIdArr.push(this.selection[i].recordId);
				}
				const loading = this.$loading();
				var res = await this.$API.finance.expense.deleteExpenseComConfigByIds.post({ids: recordIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//表格内开关
		//搜索
		async upsearch() {
			this.$refs.table.upData(this.search)
		},
		filterChange(data){
			this.search.expenseYear = data.expenseYear;
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
